﻿using System;
using System.Data.SqlTypes;
using System.DirectoryServices.Protocols;
using ActiveDirectoryUtilities.CommonLibrary;
using ActiveDirectoryUtilities.DirectoryServiceAccess;

namespace AccessActiveDirectory
{
	public class sQLSharedLogic
	{
		internal static void objectClass_User_SharedLogic(
			SearchResultEntry sre
			, out SqlGuid objectGuid
			, out SqlString distinguishedName
			, out SqlString sAMAccountName
			, out SqlDateTime lastLogonTimestamp
			, out SqlDateTime pwdLastSet
			, out SqlInt32 userAccountControl
			, out SqlBoolean ADS_UF_SCRIPT
			, out SqlBoolean ADS_UF_ACCOUNTDISABLE
			, out SqlBoolean ADS_UF_HOMEDIR_REQUIRED
			, out SqlBoolean ADS_UF_LOCKOUT
			, out SqlBoolean ADS_UF_PASSWD_NOTREQD
			, out SqlBoolean ADS_UF_PASSWD_CANT_CHANGE
			, out SqlBoolean ADS_UF_ENCRYPTED_TEXT_PWD_ALLOWED
			, out SqlBoolean ADS_UF_TEMP_DUPLICATE_ACCOUNT
			, out SqlBoolean ADS_UF_NORMAL_ACCOUNT
			, out SqlBoolean ADS_UF_INTERDOMAIN_TRUST_ACCOUNT
			, out SqlBoolean ADS_UF_WORKSTATION_TRUST_ACCOUNT
			, out SqlBoolean ADS_UF_SERVER_TRUST_ACCOUNT
			, out SqlBoolean ADS_UF_DONT_EXPIRE_PASSWD
			, out SqlBoolean ADS_UF_MNS_LOGON_ACCOUNT
			, out SqlBoolean ADS_UF_SMARTCARD_REQUIRED
			, out SqlBoolean ADS_UF_TRUSTED_FOR_DELEGATION
			, out SqlBoolean ADS_UF_NOT_DELEGATED
			, out SqlBoolean ADS_UF_USE_DES_KEY_ONLY
			, out SqlBoolean ADS_UF_DONT_REQ_PREAUTH
			, out SqlBoolean ADS_UF_PASSWORD_EXPIRED
			, out SqlBoolean ADS_UF_TRUSTED_TO_AUTH_FOR_DELEGATION
			, out SqlBoolean ADS_UF_PARTIAL_SECRETS_ACCOUNT
		)
		{
			if (sre.Attributes.Contains("objectGuid"))
			{
				objectGuid = GlobalMethods.ConvertDirectoryAttributeByteArrayToGuid(sre.Attributes["objectGuid"]);
				if (objectGuid == Guid.Empty)
				{
					objectGuid = SqlGuid.Null;
				}
			}
			else
			{
				objectGuid = SqlGuid.Null;
			}

			if (sre.Attributes.Contains("distinguishedName"))
			{
				distinguishedName = sre.Attributes["distinguishedName"][0].ToString();
			}
			else
			{
				distinguishedName = SqlString.Null;
			}

			if (sre.Attributes.Contains("sAMAccountName"))
			{
				sAMAccountName = sre.Attributes["sAMAccountName"][0].ToString();
			}
			else
			{
				sAMAccountName = SqlString.Null;
			}

			if (sre.Attributes.Contains("lastLogonTimestamp"))
			{

				DateTime tempTime = GlobalMethods.ConvertInt64ToDateTime(sre.Attributes["lastLogonTimestamp"]);
				if (DateTime.Compare(tempTime, (DateTime)SqlDateTime.MinValue) < 0)
				{
					lastLogonTimestamp = SqlDateTime.Null;
				}
				else
				{
					lastLogonTimestamp = tempTime;
				}
			}
			else
			{
				lastLogonTimestamp = SqlDateTime.Null;
			}

			if (sre.Attributes.Contains("pwdLastSet"))
			{
				DateTime tempTime = GlobalMethods.ConvertInt64ToDateTime(sre.Attributes["pwdLastSet"]);
				if (DateTime.Compare(tempTime, (DateTime)SqlDateTime.MinValue) < 0)
				{
					pwdLastSet = SqlDateTime.Null;
				}
				else
				{
					pwdLastSet = tempTime;
				}
			}
			else
			{
				pwdLastSet = SqlDateTime.Null;
			}

			int tempUserAccountControl = 0;
			if (sre.Attributes.Contains("userAccountControl"))
			{
				tempUserAccountControl = Convert.ToInt32(sre.Attributes["userAccountControl"][0]);
				userAccountControl = tempUserAccountControl;
			}
			else
			{
				userAccountControl = SqlInt32.Null;
			}


			ADS_UF_SCRIPT = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.SCRIPT) > 0;
			ADS_UF_ACCOUNTDISABLE = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.ACCOUNTDISABLE) > 0;
			ADS_UF_HOMEDIR_REQUIRED = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.HOMEDIR_REQUIRED) > 0;
			ADS_UF_LOCKOUT = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.LOCKOUT) > 0;
			ADS_UF_PASSWD_NOTREQD = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.PASSWD_NOTREQD) > 0;
			ADS_UF_PASSWD_CANT_CHANGE = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.PASSWD_CANT_CHANGE) > 0;
			ADS_UF_ENCRYPTED_TEXT_PWD_ALLOWED = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.ENCRYPTED_TEXT_PASSWORD_ALLOWED) > 0;
			ADS_UF_TEMP_DUPLICATE_ACCOUNT = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.TEMP_DUPLICATE_ACCOUNT) > 0;
			ADS_UF_NORMAL_ACCOUNT = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.NORMAL_ACCOUNT) > 0;
			ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.INTERDOMAIN_TRUST_ACCOUNT) > 0;
			ADS_UF_WORKSTATION_TRUST_ACCOUNT = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.WORKSTATION_TRUST_ACCOUNT) > 0;
			ADS_UF_SERVER_TRUST_ACCOUNT = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.SERVER_TRUST_ACCOUNT) > 0;
			ADS_UF_DONT_EXPIRE_PASSWD = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.DONT_EXPIRE_PASSWD) > 0;
			ADS_UF_MNS_LOGON_ACCOUNT = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.MNS_LOGON_ACCOUNT) > 0;
			ADS_UF_SMARTCARD_REQUIRED = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.SMARTCARD_REQUIRED) > 0;
			ADS_UF_TRUSTED_FOR_DELEGATION = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.TRUSTED_FOR_DELEGATION) > 0;
			ADS_UF_NOT_DELEGATED = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.NOT_DELEGATED) > 0;
			ADS_UF_USE_DES_KEY_ONLY = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.USE_DES_KEY_ONLY) > 0;
			ADS_UF_DONT_REQ_PREAUTH = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.DONT_REQUIRE_PREAUTH) > 0;
			ADS_UF_PASSWORD_EXPIRED = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.PASSWORD_EXPIRED) > 0;
			ADS_UF_TRUSTED_TO_AUTH_FOR_DELEGATION = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION) > 0;
			ADS_UF_PARTIAL_SECRETS_ACCOUNT = ((Ads_User_Flags)tempUserAccountControl & Ads_User_Flags.PARTIAL_SECRETS_ACCOUNT) > 0;
		}
	}
}